package com.wang.tool.monitor.fastover;

import com.wang.tool.monitor.LinkLogUtil;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.monitor.translator.GlobalErrorTranslator;
import com.wang.tool.common.ErrorInfo;
import com.wang.tool.common.Pair;
import com.wang.tool.util.SimpleLogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description 快速结束工厂
 * @Author shaopeng
 * @Date 2023/3/13
 * @Version 1.0
 */
public class FastoverFactory {

    private static final Map<String, Pair<FastoverConfig, FastoverFunction>> functionMap = new HashMap<>();

    /**
     * 取处理器
     *
     * @param key
     * @return
     */
    public static FastoverFunction getFastoverFunction(String key) {
        if (key == null) {
            return null;
        }
        Pair<FastoverConfig, FastoverFunction> functionPair = functionMap.get(key);
        return functionPair == null ? null : functionPair.getRight();
    }


    /**
     * 快速返回
     *
     * @param key    名称
     * @param time   时间戳
     * @param params 参数
     * @return
     */
    public static FastoverInfo fastover(String key, long time, Object[] params) {
        FastoverFunction fastoverFunction = getFastoverFunction(key);
        if (fastoverFunction == null) {
            return null;
        }
        try {
            return fastoverFunction.fastover(time, params);
        } catch (Throwable e) {
            ErrorInfo error = GlobalErrorTranslator.translate(e);
            LinkLogUtil.newTagLog("FastoverError")
                    .v1(key)
                    .v2(error.getErrorCode())
                    .addAttachment(MonitorInfo.ERROR, error)
                    .log();
            return null;
        }
    }


    /**
     * 注册
     *
     * @param key    非空
     * @param config 配置 非空
     * @return
     */
    public synchronized static void setFunction(String key, FastoverConfig config, FastoverFunction function) {
        if (config == null || key == null) {
            throw new IllegalArgumentException("key/config can not be null");
        }
        Pair<FastoverConfig, FastoverFunction> old = functionMap.get(key);
        if (config.equals(old.getLeft())) {
            SimpleLogUtil.info("reRegister FastoverFunction ignore, config equals, key:" + key);
            return;
        }
        if (function == null) {
            functionMap.remove(key);
            SimpleLogUtil.info("[removeFastoverFunction] key:{0}", key);
            return;
        }
        functionMap.put(key, new Pair<>(config, function));
        SimpleLogUtil.info("[changeFastoverFunction] key:{0}, newFuntion:{1}, oldFunction:{2}", key, function, old.getRight());
    }

    /**
     * 删除指定key
     *
     * @param key
     */
    public synchronized static void remove(String key) {
        if (key == null) {
            return;
        }
        functionMap.remove(key);
        SimpleLogUtil.info("[removeFastoverFunction] key:{0}", key);
    }


    /**
     * 清空所有function
     */
    public synchronized static void clear() {
        functionMap.clear();
        SimpleLogUtil.info("[clearFastoverFunction]");
    }


    /**
     * 拷贝一份配置表
     * 用于筛选&小变更配置
     *
     * @return
     */
    public static Map<String, Pair<FastoverConfig, FastoverFunction>> copyFunctions() {
        return new HashMap<>(functionMap);
    }


}
